
import com.conformiq.qtronic2.Checkpoint;
import com.conformiq.qtronic2.NotificationSink;
import com.conformiq.qtronic2.QMLArray;
import com.conformiq.qtronic2.QMLBoolean;
import com.conformiq.qtronic2.QMLNumber;
import com.conformiq.qtronic2.QMLOptional;
import com.conformiq.qtronic2.QMLRecord;
import com.conformiq.qtronic2.QMLRecordField;
import com.conformiq.qtronic2.QMLString;
import com.conformiq.qtronic2.QMLValue;
import com.conformiq.qtronic2.QMLValueVisitor;
import com.conformiq.qtronic2.ScriptBackend;
import com.conformiq.qtronic2.TimeStamp;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;
public class SeleniumScriptingBackend extends ScriptBackend {
    private NotificationSink mSink = null;
    private SeleniumAdapter seleniumAdapter;
    private String sut_url;
    private String browser;
    private String speed;
    
@Override
  public boolean beginScript(String dc_name)
    {
        mSink.notify("info", "Beginning script: " + dc_name);
        try {
            seleniumAdapter = new SeleniumAdapter();
            seleniumAdapter.setUp(sut_url, "*"+browser);
            seleniumAdapter.getSeleniumObj().setSpeed(speed);
        } catch (Exception e) {
            mSink.notify("error", e.getMessage());
        }
        return true;
    }
    @Override
  public boolean beginCase(String tc_name)
    {
        mSink.notify("info", "Beginning test case: " + tc_name);
        try {
            seleniumAdapter.getSeleniumObj().open(sut_url);
        } catch (Exception e) {
            mSink.notify("error", e.getMessage());
        }
        return true;
    }
    @Override
  public boolean checkpointInfo(Checkpoint cp, int status, TimeStamp ts)
    {
        return true;
    }
    @Override
    public boolean testStep(QMLRecord datum, String thread, String port,
                               boolean isFromTester, TimeStamp ts)
      {
          StringBuffer data = new StringBuffer();
          ValueVisitor v = new ValueVisitor(data);
          datum.accept(v);
          try {
        	  Thread.sleep(1000);
              if (datum.getName().equals("click")){
                 if (datum.getField("url").getValue().toString().substring(14).equals("ChiSiamo")){
            	     Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Chi siamo");
                     seleniumAdapter.getSeleniumObj().click("link=Chi siamo");
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("Home")){
             	     Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Home");
                     seleniumAdapter.getSeleniumObj().click("link=Home");
                 } 
                 else if(datum.getField("url").getValue().toString().substring(14).equals("Privati")){
             	     Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Privati");
                     seleniumAdapter.getSeleniumObj().click("link=Privati");
                 } 
                 else if(datum.getField("url").getValue().toString().substring(14).equals("Aziende")){
              	     Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Aziende");
                     seleniumAdapter.getSeleniumObj().click("link=Aziende");
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("Contatti")){
               	     Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Contatti");
                     seleniumAdapter.getSeleniumObj().click("link=Contatti");
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("Privacy Policy")){
                 	 Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Privacy Policy");
                     seleniumAdapter.getSeleniumObj().click("link=Privacy Policy");        
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("IscriviPrivato")){
                 	 Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Registrati");
                     seleniumAdapter.getSeleniumObj().click("link=Registrati");        
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("IscriviPrivato2")){
                 	 Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=privato");
                     seleniumAdapter.getSeleniumObj().click("link=privato");        
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("RegistraAzienda")){
                 	 Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Registrati");
                     seleniumAdapter.getSeleniumObj().click("link=Registrati");        
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("RegistraAzienda2")){
                 	 Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=azienda!");
                     seleniumAdapter.getSeleniumObj().click("link=azienda!");        
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("Login")){
                	 Thread.sleep(1000);
                     seleniumAdapter.getSeleniumObj().highlight("link=Login");
                     seleniumAdapter.getSeleniumObj().click("link=Login");       
                 }
                 else if(datum.getField("url").getValue().toString().substring(14).equals("Ricerca")){
             	     Thread.sleep(1000);
             	    seleniumAdapter.getSeleniumObj().click("//input[@type='submit' and @value='Cerca']");
                 }                  
              } 
              if (datum.getName().equals("RegistrationUserData")){	 
             	  String nome = datum.getField("name").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=nome", nome);
                  
                  String cognome = datum.getField("surname").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=cognome", cognome);
                  
                  seleniumAdapter.getSeleniumObj().click("name=sesso value=M");
                  
                  String dataN = datum.getField("birthdate").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=datanascita", dataN);
                
                  String indirizzo = datum.getField("address").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=indirizzo", indirizzo);
                  
                  String cittadinanza = datum.getField("nationality").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=cittadinanza", cittadinanza);
                  
                  String indemail = datum.getField("email").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=email", indemail);
                  
                  String pwd = datum.getField("password").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=password", pwd);
                  seleniumAdapter.getSeleniumObj().type("name=controllopassword", pwd);
                   
                  String voto = datum.getField("vote").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=voto", voto);
                  
                  String istituzione = datum.getField("institution").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=istituzione", istituzione);
                  
                  String dataE = datum.getField("dateS").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=data", dataE);
                  
                  seleniumAdapter.getSeleniumObj().click("name=tipologia value=1");
                  seleniumAdapter.getSeleniumObj().click("name=trasferte value=1");
                  Thread.sleep(1000);
                  seleniumAdapter.getSeleniumObj().click("name=Invia");
              }
              if (datum.getName().equals("RegistrationCompanyData")){	
                  Thread.sleep(1000);
          	      String rs = datum.getField("ragsoc").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=ragionesociale", rs);
              
                  String email = datum.getField("email").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=email", email);
              
                  String password = datum.getField("pwd").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=password", password);
                  seleniumAdapter.getSeleniumObj().type("name=controllopassword", password);
              
                  String sede = datum.getField("headquarter").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=sede", sede);
                  
                  String numtel = datum.getField("phone").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=telefono", numtel);
      
                  Thread.sleep(1000);
                  seleniumAdapter.getSeleniumObj().click("name=Invia");
              }
              if (datum.getName().equals("LoginData")){
            	  
            	  String us = datum.getField("user").getValue().toString().substring(14);

                  seleniumAdapter.getSeleniumObj().type("name=pippo", us);
                  String pwd = datum.getField("pass").getValue().toString().substring(14);
                  seleniumAdapter.getSeleniumObj().type("name=pluto", pwd);

                  Thread.sleep(1000);
                  seleniumAdapter.getSeleniumObj().click("name=paperino");
              }
              if (datum.getName().equals("page")){
                 if(datum.getField("name").getValue().toString().substring(14).equals("ChiSiamo")){
            	     Thread.sleep(1000);
                     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("chisiamo")){ 
           	        	mSink.notify("info","ID FR 4-ChiSiamo: OK");
           	         }
           	         else{
           	        	mSink.notify("error","ID FR 4-ChiSiamo: ERRORE");	 
           	         }  
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("Home")){
        	         Thread.sleep(1000);
                     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("home")){
           	        	mSink.notify("info","ID FR 4-Home: OK");
           	         }
           	         else{
           	        	mSink.notify("error","ID FR 4-Home: ERRORE");	 
           	         }  
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("Privati")){
          	         Thread.sleep(1000);
                     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("privati")){ 
          	        	mSink.notify("info","ID FR 4-Privati: OK");
          	         }
          	         else{
          	        	mSink.notify("error","ID FR 4-Privati: ERRORE");	 
          	         }  
                 } 
                 else if(datum.getField("name").getValue().toString().substring(14).equals("Aziende")){
             	     Thread.sleep(1000);
                     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("aziende")){ 
           	        	mSink.notify("info","ID FR 4-Aziende: OK");
           	         }
           	         else{
           	        	mSink.notify("error","ID FR 4-Aziende: ERRORE");	 
           	         }  
                 } 
                 else if(datum.getField("name").getValue().toString().substring(14).equals("Contatti")){
              	     Thread.sleep(1000);
                     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("contatti")){ 
           	        	mSink.notify("info","ID FR 4-Contatti: OK");
           	         }
           	         else{ 
           	        	mSink.notify("error","ID FR 4-Contatti: ERRORE");	 
           	         }  
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("Privacy Policy")){
               	     Thread.sleep(1000);
                     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("privacy")){
           	        	mSink.notify("info","ID FR 4-Privacy: OK");
           	         }
           	         else{
           	        	mSink.notify("error","ID FR 4-Privacy: ERRORE");	 
           	         }  
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("ErrorUP")){
               	     Thread.sleep(1000);
               	     if(seleniumAdapter.getSeleniumObj().isElementPresent("id=errore")){ 
         	        	mSink.notify("info","ID FR 7-User: OK"); 
         	         }
         	         else{ 
         	        	mSink.notify("error","ID FR 7-User: ERRORE");	
         	         }    
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("SuccessUP")){
               	     Thread.sleep(1000);
               	       if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("profilo")){ 
         	        	mSink.notify("info","ID FR 1: OK");
         	        	mSink.notify("info","ID FR 6: OK");
         	           }
               	       else{
         	        	mSink.notify("error","ID FR 1: ERROR");
         	        	mSink.notify("error","ID FR 6: ERROR");	
         	           }
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("ErrorAZ")){
               	     Thread.sleep(1000);
               	     if(seleniumAdapter.getSeleniumObj().isElementPresent("id=errore")){
         	        	mSink.notify("info","ID FR 7-Company: OK"); 
         	         }
         	         else{
         	        	mSink.notify("error","ID FR 7-Company: ERRORE");	
         	         }
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("SuccessAZ")){
               	     Thread.sleep(1000);
               	     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("profilo")){ 
         	           mSink.notify("info","ID FR 5: OK");
         	         }
               	     else{ 
         	           mSink.notify("error","ID FR 5: ERRORE");	
         	         }
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("profiloAdmin")){
                	 Thread.sleep(1000);
               	     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("profilo")){ 
         	           mSink.notify("info","ID FR 2-Admin: OK");
         	         }
               	     else{
         	           mSink.notify("error","ID FR 2-Admin: ERRORE");	
         	         }
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("profiloUser")){
                	 Thread.sleep(1000);
               	     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("profilo")){
         	           mSink.notify("info","ID FR 2-User: OK");
         	         }
               	     else{
         	           mSink.notify("error","ID FR 2-User: ERRORE");	
         	         }
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("profiloCompany")){
                	 Thread.sleep(1000);
               	     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("profilo")){ 
         	           mSink.notify("info","ID FR 2-Company: OK");
         	         }
               	     else{ 
         	           mSink.notify("error","ID FR 2-Company: ERRORE");	
         	         }
                 }
                 else if(datum.getField("name").getValue().toString().substring(14).equals("Ricerca")){
       	             Thread.sleep(1000); 
               	     if(seleniumAdapter.getSeleniumObj().getLocation().toString().contains("ricerca")){ 
         	           mSink.notify("info","ID FR 10: OK");
         	         }
               	     else{ 
         	           mSink.notify("error","ID FR 10: ERRORE");	
         	         }
                 }                 
           }     
          } catch (Exception e) {
              mSink.notify("error", e.getMessage());
          }
          return true;
      }
      @Override
    public boolean internalCommunicationsInfo(QMLRecord datum, String sender,
                                                 String receiver, String port,
                                                 TimeStamp ts)
      {
          return true;
      }
      @Override
    public boolean endCase()
      {
          mSink.notify("info", "Ending test case");
          return true;
      }
      @Override
    public boolean endScript()
      {
          mSink.notify("info", "Ending script");
          return true;
      }
      @Override
    public void caseProbability(double probability)
      {
      }
      @Override
      public boolean setConfigurationOption(String property, String value)
      {
          mSink.notify("info", "Set configuration option '" + property +
                  " to '" + value + "'");
          if ("SeleniumConfig.sut_url".equals(property))
              sut_url = value;
          else if ("SeleniumConfig.browser".equals(property))
              browser = value;
          else if ("SeleniumConfig.speed".equals(property))
              speed = value;
          return true;
      }
      @Override
    public boolean trace(String message, TimeStamp time) { return true; }
      @Override
    public void setNotificationSink(NotificationSink sink) { mSink = sink; }
      /** Value visitor for rendering QML values. */
      class ValueVisitor implements QMLValueVisitor {
          public ValueVisitor(StringBuffer out) { 
              mOut = out;
          }
          /** Visit a QML array. */
          public void visit(QMLArray a)
          {
              mOut.append("{ ");
              int n = a.getNumberOfElements();
              if (n < 1)
              {
                   mOut.append("'empty array'");
              }
              else
              {
                   for (int i = 0; i < n; i++)
                   {
                       if (i > 0)
                       {
                           mOut.append(", ");
                       }
                       a.getValue(i).accept(this);
                   }
              }
              mOut.append(" }");
          }
          /** Visit a QML boolean. */
          public void visit(QMLBoolean b)
          {
              mOut.append(b.getValue() ? "true" : "false");
          }
          /** Visit a QML number. */
          public void visit(QMLNumber n)
          {
              if (n.isBigInteger())
              {
                   mOut.append(n.getBigInteger());
              }
              else
              {
                   mOut.append(n.getNumerator());
                   mOut.append("/");
                   mOut.append(n.getDenominator());
              }
          }
          /** Visit a QML record. */
          public void visit(QMLRecord r)
          {
              for (int i = 0, n = r.getNumberOfFields(); i < n; i++)
              {
                   QMLRecordField f = r.getField(i);
                   QMLValue v = f.getValue();
                   if (v != null)
                   {
                        v.accept(this);
                   }
                   else
                   {
                        mOut.append("null");
                   }
              }
          }
          /** Visit a QML string. */
          public void visit(QMLString s)
          {
              if (s.getValue().equals(""))
              {
                   mOut.append("\"\"");
              }
              else
              {
                   mOut.append("\"" + s.getValue() + "\"");
              }
          }
          /** Visit a QML optional type. */
          public void visit(QMLOptional p)
          {
              if (p.isPresent())
              {
                   mOut.append("optional: ");
                   p.getValue().accept(this);
              }
              else
              {
                   mOut.append("'omitted field'");
              }
          }
          private StringBuffer mOut = null;
      }
      
  public class SeleniumAdapter extends SeleneseTestCase {
          
  public Selenium getSeleniumObj(){
              return this.selenium;
          }
  }
}
              